//
//  并查集1.swift
//  Swift-LeetCode
//
//  Created by 卢悦明 on 2024/3/2.
//

import UIKit

/// 第一种实现
class UnionFind_QF: UnionFind {
    /// 查找v的根节点
    /// - Parameter v:
    /// - Returns: 结果
    override func find(_ v: Int) -> Int {
        return parents[v]
    }
    
    /// 合并v1和v2
    /// - Parameters:
    ///   - v1: 1
    ///   - v2: 2
    override func union(_ v1: Int, _ v2: Int) {
        let p1 = find(v1)
        let p2 = find(v2)
        if p1 == p2 {
            return
        }
        var index = 0
        while index < parents.count {
            if parents[index] == p1 {
                parents[index] = p2
            }
            index += 1
        }
    }
}
